﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Invini.Training
{
    public class PathComponentsCollection : List<IPathComponent>
    {
        public IEnumerable<object> FindElements()
        {
            Queue<IPathComponent> pathComponents = new Queue<IPathComponent>(this);
            Queue<object> objects = new Queue<object>(Application.Current.Windows.OfType<object>());

            while (pathComponents.Count > 0)
            {
                IPathComponent pathComponent = pathComponents.Dequeue();

                Queue<object> nextObjects = new Queue<object>();
                while (objects.Count > 0)
                {
                    IEnumerable<object> children = pathComponent.FindElements(objects.Dequeue());
                    if (children == null)
                        continue;

                    foreach(object child in children)
                        nextObjects.Enqueue(child);
                }

                objects = nextObjects;
            }

            return objects.ToArray();
        }
    }
}
